﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;

namespace ETL.Business.Handlers.ProjectTimeHandler
{
    public class GetAllProjectTimes
    {
        private readonly ProjectTimeRepository _projectTimeRepository;

        public GetAllProjectTimes(ProjectTimeRepository projectTimeRepository)
        {
            _projectTimeRepository = projectTimeRepository;
        }
        public GetAllProjectTimes()
        {
            _projectTimeRepository = new ProjectTimeRepository();
        }
        public virtual IEnumerable<ProjectTimeModel> Execute(string userId, DateTime beforeDate)
        {
            IEnumerable<ProjectTimeModel> projectTimeModel = null;
            try
            {
                // try to get from db
                IEnumerable<project_times> projectTimesData = _projectTimeRepository.GetAllProjectTimes(userId, beforeDate);

                // parse to business model
                if (projectTimesData != null)
                {
                    projectTimeModel = from s in projectTimesData select new ProjectTimeModel(s);
                }
                return projectTimeModel;
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
                return null;
            }
        }
    }
}
